<?php

use app\common\enum\DeliveryType as DeliveryTypeEnum;

?>
<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-head am-cf">
                    <div class="widget-title am-cf"><?= $title ?></div>
                </div>
                <div class="widget-body am-fr">
                    <!-- 工具栏 -->
                    <div class="page_toolbar am-margin-bottom-xs am-cf">
                        <form id="form-search" class="toolbar-form" action="">
                            <input type="hidden" name="s" value="/<?= $request->pathinfo() ?>">
                            <input type="hidden" name="dataType" value="<?= $dataType ?>">
                            <div class="am-u-sm-12 am-u-md-3">
                                <div class="am-form-group">
                                    <div class="am-btn-toolbar">
                                        <div class="am-btn-group am-btn-group-xs">
                                            <?php if (checkPrivilege('order.operate/export')): ?>
                                                <a class="j-export am-btn am-btn-success am-radius"
                                                   href="javascript:void(0);">
                                                    <i class="iconfont icon-daochu am-margin-right-xs"></i>订单导出
                                                </a>
                                            <?php endif; ?>
                            
                                        </div>
                                    </div>
                                </div>
                            </div>
                            
                            <div class="am-u-sm-12 am-u-md-9">
                                <div class="am fr">
                                    <div class="am fr">
                                        <?php if ($title == "全部订单列表"): ?>
                                        <?php $pay_status = $request->get('pay_status') ?: null; ?>
                                         <!-- <div class="am-form-group tpl-form-border-form am-fl">
                                            <?php $sta = $request->get('sta') ?: null; ?>
                                            <select name="sta"
                                                    data-am-selected="{searchBox: 1, btnSize: 'sm',  placeholder: '医生/项目', maxHeight: 400}">
                                                <option value="">医生/项目</option>
                                                <option value="全部" <?= $sta == '' ? 'selected' : ''?> >全部</option>
                                                <option value="20"<?= $sta == '20' ? 'selected' : ''?> >医生</option>
                                                <option value="10"<?= $sta == '10' ? 'selected' : ''?> >项目</option>
                                                
                                            </select>
                                        </div> -->
                                        <div class="am-form-group tpl-form-border-form am-fl">
                                            <select name="pay_status"
                                                    data-am-selected="{searchBox: 1, btnSize: 'sm',  placeholder: '付款状态', maxHeight: 400}">
                                                <option value="">付款状态</option>
                                                <option value="全部" <?= $pay_status == '全部' ? 'selected' : ''?> >全部</option>
                                                <option value="20"<?= $pay_status == '20' ? 'selected' : ''?> >已付款</option>
                                                <option value="10"<?= $pay_status == '10' ? 'selected' : ''?> >待付款</option>
                                                <option value="30"<?= $pay_status == '30' ? 'selected' : ''?> >已退款</option>
    
                                            </select>
                                        </div>
                                        <div class="am-form-group tpl-form-border-form am-fl">
                                            <?php $receipt_status = $request->get('receipt_status') ?: null; ?>
                                            <select name="receipt_status"
                                                    data-am-selected="{searchBox: 1, btnSize: 'sm',  placeholder: '就诊状态', maxHeight: 400}">
                                                <option value="">就诊状态</option>
                                                <option value="全部" <?= $receipt_status == '全部' ? 'selected' : ''?> >全部</option>
                                                <option value="20"<?= $receipt_status == '20' ? 'selected' : ''?> >已就诊</option>
                                                <option value="10"<?= $receipt_status == '10' ? 'selected' : ''?> >待就诊</option>
                                                
                                            </select>
                                        </div>
                                        <?php endif; ?>
                                         <div class="am-form-group tpl-form-border-form am-fl">
                                            <?php $sta = $request->get('sta') ?: null; ?>
                                            <select name="sta"
                                                    data-am-selected="{searchBox: 1, btnSize: 'sm',  placeholder: '医生/项目', maxHeight: 400}">
                                                <option value="">医生/项目</option>
                                                <option value="全部" <?= $sta == '' ? 'selected' : ''?> >全部</option>
                                                <option value="20"<?= $sta == '20' ? 'selected' : ''?> >医生</option>
                                                <option value="10"<?= $sta == '10' ? 'selected' : ''?> >项目</option>
                                                
                                            </select>
                                        </div>
                                        <div class="am-form-group tpl-form-border-form am-fl">
                                            <input type="text" name="start_time"
                                                class="am-form-field"
                                                value="<?= $request->get('start_time') ?>" placeholder="请选择起始日期"
                                                data-am-datepicker>
                                        </div>
                                        <div class="am-form-group tpl-form-border-form am-fl">
                                            <input type="text" name="end_time"
                                                class="am-form-field"
                                                value="<?= $request->get('end_time') ?>" placeholder="请选择截止日期"
                                                data-am-datepicker>
                                        </div>
                                        <div class="am-form-group am-fl">
                                            <div class="am-input-group am-input-group-sm tpl-form-border-form">
                                                <input type="text" class="am-form-field" name="search"
                                                    placeholder="请输入订单号/用户昵称/手机号/姓名/推荐人" value="<?= $request->get('search') ?>">
                                                <div class="am-input-group-btn">
                                                    <button class="am-btn am-btn-default am-icon-search"
                                                            type="submit"></button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="order-list am-scrollable-horizontal am-u-sm-12 am-margin-top-xs">
                        <table width="100%" class="am-table am-table-centered
                        am-text-nowrap am-margin-bottom-xs">
                            <thead>
                            <tr>
                                <th width="25%" class="goods-detail">挂号信息</th>
                                <th width="10%">挂号费</th>
                                <th width="15%">实付款</th>
                                <th>推荐人</th>
                                <th>预约人</th>
                                <th>预约时间</th>
                                <th>支付方式</th>
                                <th>交易状态</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php $colspan = 8; ?>
                            <?php if (!$list->isEmpty()): foreach ($list as $order): ?>
                                <tr class="order-empty">
                                    <td colspan="<?= $colspan ?>"></td>
                                </tr>
                                <tr>
                                    <td class="am-text-middle am-text-left" colspan="<?= $colspan ?>">
                                        <span class="am-margin-right-lg"> <?= $order['create_time'] ?></span>
                                        <span class="am-margin-right-lg">订单号：<?= $order['order_no'] ?></span>
                                    </td>
                                </tr>
                                <?php $i = 0;
                                foreach ($order['goods'] as $goods): $i++; ?>
                                    <tr>
                                        <td class="goods-detail am-text-middle">
                                            <div class="goods-image">
                                                <img src="<?= $goods['image']['file_path'] ?>" alt="">
                                            </div>
                                            <div class="goods-info">
                                                <p class="goods-title">医生名称：<?= $goods['goods_name'] ?></p>
                                                <p class="goods-spec am-link-muted"><?= $goods['goods_attr'] ?></p>
                                                <p class="goods-title">科室：<?= $order['department_name'] ?></p>
                                            </div>
                                        </td>
                                        <td class="am-text-middle">
                                            <p>￥<?= $goods['goods_price'] ?></p>
                                        </td>
                                        <?php if ($i === 1) : $goodsCount = count($order['goods']); ?>
                                            <td class="am-text-middle" rowspan="<?= $goodsCount ?>">
                                                <p>￥<?= $order['pay_price'] ?></p>
                                            </td>
                                            <td class="am-text-middle" rowspan="<?= $goodsCount ?>">
                                             <p><?= $order['recommender_name'] ? $order['recommender_name'] : '平台' ?></p>
                                            </td>
                                            <td class="am-text-middle" rowspan="<?= $goodsCount ?>">
                                                <p><?= $order['user']['nickName'] ?></p>
                                                <p class="am-link-muted">(就诊人：<?= $order['jiuzhen_name'] ?>)</p>
                                                <p class="am-link-muted">(电话：<?= $order['jiuzhen_phone'] ?>)</p>
                                            </td>
                                            <!-- <td class="am-text-middle" rowspan="<?= $goodsCount ?>">
                                                <p><?= $order['jiuzhen_day'] ?></p>
                                                <p><?= $order['jiuzhen_time'] ?></p>
                                            </td> -->
                                            <td class="am-text-middle" rowspan="<?= $goodsCount ?>">
                                                <p><?= $order['jiuzhen_day'] ?></p>
                                                <p>
                                                <?= $order['jiuzhen_time'] ?>
                                                    <!-- <?php
                                                    // 提取 jiuzhen_time 并判断上午或下午
                                                    if (!empty($order['jiuzhen_time'])) {
                                                        // 拆分时间字符串，获取开始时间
                                                        $timeRange = explode('-', $order['jiuzhen_time']);
                                                        $startTime = strtotime($timeRange[0]);

                                                        // 判断上午或下午
                                                        if ($startTime < strtotime('12:00')) {
                                                            echo '上午';
                                                        } else {
                                                            echo '下午';
                                                        }
                                                    } else {
                                                        echo '时间未指定';
                                                    }
                                                    ?> -->
                                                </p>
                                            </td>
                                            <td class="am-text-middle" rowspan="<?= $goodsCount ?>">
                                                <span class="am-badge am-badge-secondary">
                                                    <?= $order['pay_type']['text'] ?>
                                                </span>
                                            </td>
                                            <td class="am-text-middle" rowspan="<?= $goodsCount ?>">
                                                <p>付款状态：
                                                    <span class="am-badge
                                                <?= $order['pay_status']['value'] == 20 ? 'am-badge-success' : '' ?>">
                                                        <?= $order['pay_status']['text'] ?></span>
                                                </p>
                                                <p>就诊状态：
                                                    <span class="am-badge
                                                <?= $order['receipt_status']['value'] == 20 ? 'am-badge-success' : '' ?>">
                                                        <?= $order['receipt_status']['text'] ?></span>
                                                </p>
                                                <?php if ($order['order_status']['value'] == 20 || $order['order_status']['value'] == 21 || !empty($order['order_refund_id'])): ?>
                                                    <p>订单状态：
                                                        <span class="am-badge am-badge-warning">
                                                            <?= !empty($order['order_refund_id']) ? '已取消' : $order['order_status']['text'] ?>
                                                        </span>
                                                    </p>
                                                <?php endif; ?>
                                            </td>
                                            <td class="am-text-middle" rowspan="<?= $goodsCount ?>">
                                                <div class="tpl-table-black-operation">
                                                    <?php if (checkPrivilege('order/detail')): ?>
                                                        <a class="tpl-table-black-operation-green"
                                                           href="<?= url('order/detail', ['order_id' => $order['order_id']]) ?>">
                                                            订单详情</a>
                                                    <?php endif; ?>
                                                    <?php if (checkPrivilege(['order/detail', 'order/delivery'])): ?>
                                                        <?php if ($order['pay_status']['value'] == 20
                                                            && $order['delivery_status']['value'] == 20
                                                            && $order['order_status']['value'] != 20
                                                            && $order['order_status']['value'] != 21
                                                            && $order['order_status']['value'] != 30
                                                            && $order['order_refund_id'] == null
                                                            
                                                        ): ?>
                                                            <a class="item-delete1 tpl-table-black-operation-del"
                                                               href="javascript:;" data-id="<?= $order['order_id'] ?>">确认到诊</a>
                                                        <?php endif; ?>
                                                    <?php endif; ?>
                                                    <?php if (checkPrivilege(['order/detail', 'order/delivery'])): ?>
                                                        <?php if ($order['pay_status']['value'] == 20
                                                            && $order['order_refund_id'] == null
                                                            && $order['order_status']['value'] == 10
                                                            // && $order['order_status']['value'] != 21
                                                        ): ?>
                                                         <a href="javascript:;" class="item-delete tpl-table-black-operation-del"
                                                            data-id="<?= $goods['order_goods_id'] ?>">
                                                                <i class="am-icon-trash"></i> 操作退款
                                                            </a>
                                                        <?php endif; ?>
                                                    <?php endif; ?>
                                                    <?php if (checkPrivilege(['order/detail', 'order.operate/confirmcancel'])): ?>
                                                        <?php if ($order['order_status']['value'] == 21): ?>
                                                            <a class="tpl-table-black-operation-del"
                                                               href="<?= url('order/detail#cancel',
                                                                   ['order_id' => $order['order_id']]) ?>">去审核</a>
                                                        <?php endif; ?>
                                                    <?php endif; ?>
                                                </div>
                                            </td>
                                        <?php endif; ?>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endforeach; else: ?>
                                <tr>
                                    <td colspan="<?= $colspan ?>" class="am-text-center">暂无记录</td>
                                </tr>
                            <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                    <div class="am-u-lg-12 am-cf">
                        <div class="am-fr"><?= $list->render() ?> </div>
                        <div class="am-fr pagination-total am-margin-right">
                            <!-- <div class="am-vertical-align-middle">显示第N到第X条记录，总共<?= $list->total() ?>条记录，每页显示H条记录</div> -->
                            <div class="am-vertical-align-middle">
                                <?php
                                $currentPage = $list->currentPage(); // 当前页
                                $listRows = $list->listRows(); // 每页记录数
                                $total = $list->total(); // 总记录数
                                $start = ($currentPage - 1) * $listRows + 1; // 当前页开始记录
                                $end = min($currentPage * $listRows, $total); // 当前页结束记录
                                ?>
                                显示第 <?= $start ?> 到第 <?= $end ?> 条记录，总共 <?= $total ?> 条记录，每页显示
                                <select onchange="changePageSize(this.value)">
                                    <?php foreach ([10,20, 50, 100] as $size): ?>
                                        <option value="<?= $size ?>" <?= $listRows == $size ? 'selected' : '' ?>><?= $size ?> 条</option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    function changePageSize(size) {
        var url = new URL(window.location.href);
        url.searchParams.set('listRows', size); // 设置每页显示条数
        url.searchParams.set('page', 1); // 重置到第一页
        window.location.href = url.toString(); // 跳转
    }
</script>
<script>

    $(function () {

        /**
         * 订单导出
         */
        $('.j-export').click(function () {
            var data = {};
            var formData = $('#form-search').serializeArray();
            $.each(formData, function () {
                this.name !== 's' && (data[this.name] = this.value);
            });
            window.location = "<?= url('order.operate/export') ?>" + '&' + $.urlEncode(data);
        });
        // $('.item-delete').click(function () {
        //     var data = $(this).data();
        //     layer.confirm('确定要退号退款吗'
        //         , {title: '友情提示'}
        //         , function (index) {
        //             $.post("<?= url('order.Refund/receipt') ?>"
        //                 , {
        //                     order_goods_id: data.id,
        //                 }
        //                 , function (result) {
        //                     result.code === 1 ? $.show_success(result.msg, result.url)
        //                         : $.show_error(result.msg);
        //                 });
        //             layer.close(index);
        //         });
        // });
        $('.item-delete').click(function () {
            var data = $(this).data();
            layer.confirm('确定要退号退款吗', { title: '友情提示' }, function (index) {
                $.post("<?= url('order.Refund/apply') ?>", {
                        order_goods_id: data.id,
                    }, function (result) {
                        if (result.code === 1) {
                            // 提示成功后跳转页面
                            $.show_success(result.msg, result.url || function () {
                                // 如果 result.url 存在，则跳转，否则刷新当前页面
                                window.location.href = result.url || window.location.href;
                            });
                        } else {
                            $.show_error(result.msg);
                        }
                    });
                layer.close(index);
            });
        });
        $('.item-delete1').click(function () {
            var data = $(this).data();
            layer.confirm('确定患者已经到诊了吗'
                , {title: '友情提示'}
                , function (index) {
                    $.post("<?= url('order/receipt') ?>"
                        , {
                            order_id: data.id,
                        }
                        , function (result) {
                            result.code === 1 ? $.show_success(result.msg, result.url)
                                : $.show_error(result.msg);
                        });
                    layer.close(index);
                });
        });
        // 删除元素
        // var url = "<?= url('order.operate/delete') ?>";
        // $('.item-delete').delete('order_id', url);
    });

</script>

